package com.desktopmng.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;

import com.desktopmng.core.support.Pager;
import com.desktopmng.exception.JFossException;
import com.desktopmng.orm.UserConfig;
import com.desktopmng.orm.UserToDstc;

@SuppressWarnings({"unchecked","rawtypes"})
public class UserConfigManager extends HibernateEntityDao<UserConfig> {

	/**
	 * Description: 新增个人配置
	 * 
	 * @param userConfig
	 *            个人配置对象
	 * @return 操作标记
	 * @throws JFossException
	 */
	public String addUserConfig(UserConfig userConfig) throws JFossException {
		String retStr = "";
		try {
			userConfig.setId(null);
			this.save(userConfig);
			retStr="true";
		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return retStr;
	}

	/**
	 * Description: 修改个人配置
	 * 
	 * @param userConfig
	 *            个人配置对象
	 * @return 操作标记
	 * @throws JFossException
	 */
	public String updateUserConfig(UserConfig userConfig) throws JFossException {
		String retStr = null;
		try {
			this.getHibernateTemplate().update(userConfig);
			retStr="true";
		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return retStr;
	}
	
	/**
	 * 
	 * Description:修改个人工作室内容配置，同时修改用户与个人工作室内容关联表
	 * @param userConfig
	 * @param ids
	 * @return
	 * @throws JFossException
	 */
	public String updateUserConfig(UserConfig userConfig, String ids) throws JFossException {
		String retStr = null;
		try {
			Long userId = userConfig.getUserId();
			Session s = getSession();
			String sql = "DELETE FROM T_JFOSS2_DESK_USER_TO_DSTC WHERE USER_ID = ?";
			s.createSQLQuery(sql).setLong(0, userId).executeUpdate();
			if(StringUtils.isNotBlank(ids)) {
				List<UserToDstc> utdList = new ArrayList<UserToDstc>();
				String[] arrIds = ids.split(",");
				for(String id : arrIds) {
					UserToDstc utd = new UserToDstc();
					utd.setId(null);
					utd.setUserId(userId);
					utd.setDstcId(new Long(id));
					utdList.add(utd);
				}
				this.getHibernateTemplate().saveOrUpdateAll(utdList);
			}
			this.getHibernateTemplate().update(userConfig);
			retStr="true";
		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return retStr;
	}	

	/**
	 * Description: 删除个人配置
	 * 
	 * @param userConfigId
	 *            个人配置对象Id
	 * @return 操作标记
	 * @throws JFossException
	 */
	public String deleteUserConfig(Long userConfigId) throws JFossException {
		String retStr = "true";
		try {
			if (null != userConfigId) {
				UserConfig pc = new UserConfig();
				pc.setId(userConfigId);
				this.getHibernateTemplate().delete(pc);
			}
		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return retStr;
	}

	/**
	 * Description: 查看个人配置对象
	 * 
	 * @param userConfigId
	 *            个人配置对象Id
	 * @return 个人配置对象
	 * @throws JFossException
	 */
	public UserConfig findUserConfigById(Long userConfigId)
			throws JFossException {
		UserConfig obj = null;
		try {
			if (null != userConfigId) {
				obj = (UserConfig) this.getHibernateTemplate().load(
						UserConfig.class, userConfigId.longValue());
			} else {
				return null;
			}
		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return obj;
	}

	/**
	 * Description: 条件查询个人配置库
	 * 
	 * 条件参数列表，为空时，返回全部个人配置列表
	 * 
	 * @param prop
	 *            条件参数表
	 * @return 封装个人配置库对象的列表
	 * @throws JFossException
	 */
	public List findUserConfigByCondition(Properties prop)
			throws JFossException {
		List list = null;
		try {

			String hql = "select p from UserConfig p where 1=1";
			List paramList = new ArrayList();
			if (null != prop && prop.size() > 0) {
				String userId = prop.getProperty("userId");
				if (StringUtils.isNotBlank(userId)) {
					hql += " and p.userId = ?";
					paramList.add(new Long(userId));
				}
			}
			hql += " order by p.id";
			list = this.find(hql, paramList.toArray());

		} catch (Exception e) {

			throw new JFossException("", e);
		}
		if (!list.isEmpty()) {
			return list;
		} else {
			return null;
		}
	}

	/**
	 * Description: 根据查询条件获取个人配置库对象列表,分页查询
	 * 
	 * @param prop
	 *            查询条件
	 * @param pageNum
	 *            页码
	 * @param pageSize
	 *            页数据量
	 * @return 个人配置库对象列表分页对象
	 * @throws JFossException
	 */
	public Pager findUserConfigByCondition(Properties prop, int pageNum,
			int pageSize) throws JFossException {
		Pager page = null;
		try {

			String hql = "select p from UserConfig p where 1=1";
			List paramList = new ArrayList();
			if (null != prop && prop.size() > 0) {
				String msStatus = prop.getProperty("userId");
				if (StringUtils.isNotBlank(msStatus)) {
					hql += " and p.userId = ?";
					paramList.add(msStatus);
				}
			}
			hql += " order by p.id";
			page = this.pagedQuery(hql, pageNum, pageSize, paramList.toArray());

		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return page;
	}

	/**
	 * Description: 根据用户的设置LAYOUT布局形式的不同，调整用户templete
	 * 
	 * @param prop
	 * 个人配置LAYOUT @ 更新数据库中的LAYOUT 和调整用户的templete
	 */
	public UserConfig updateUserConfigByLayoutNumber(UserConfig userConfig,
			int layoutNumber) throws JFossException {

		try {
			if (userConfig.getTemplate() != null) {
				if (layoutNumber == 1) {
					userConfig.setLayout("99%;");
					String Template = userConfig.getTemplate();
					String newTemp = Template.replaceAll("&", "") + "&";
					userConfig.setTemplate(newTemp);

				} else if (layoutNumber == 2) {
					userConfig.setLayout("49.5%;49.5%;");
					String Template = userConfig.getTemplate();
					String newTemp = Template.replaceAll("&", "") + "&" + "&";
					userConfig.setTemplate(newTemp);
				} else if (layoutNumber == 3) {
					userConfig.setLayout("33%;33%;33%;");
					String Template = userConfig.getTemplate();
					String newTemp = Template.replaceAll("&", "") + "&" + "&"
							+ "&";
					userConfig.setTemplate(newTemp);
				}

				this.getHibernateTemplate().update(userConfig);
			}
		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return userConfig;
	}
	
	/**
	 * 根据用户ID查询出个人配置
	 * Saihua.Yin 添加
	 * @param userId
	 * @return
	 */
	public UserConfig findUserConfigByUserId(Long userId){
		String hql = "from UserConfig p where 1=1";
		List<Object> listParam = new ArrayList<Object>();
		if(null != userId){
			hql += " and p.userId = ?";
			listParam.add(userId);
		}
		List<UserConfig> listUserConfig = createQuery(hql, listParam.toArray()).list();
		if(!listUserConfig.isEmpty() && listUserConfig.size() == 1){
			return listUserConfig.get(0);
		}else{
			return null;
		}
	}
}
